// pages/confirm_order/confirm_order.js
const getToken = require('../../utils/get_token.js');
let memberId, openId, cookie, token;
let pageSize=1;

const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    let tokens = await getToken.getToken();
    thou(that);
  }
}
const thou = (that)=>{//token没有失效时执行的代码
  if (openId == '') {
    getToken.getOpenId(function (res) {
      wx.setStorageSync('openId', res.data.openId);
      openId = res.data.openId;
      detail(that);
    });
  } else {
    detail(that);
  }
}
const detail = (that) => {
  that.setData({hidden: false})
  let params = ['shop/pingTuan/huoPinViewList', { 'openId': openId, 'memberId': memberId, 'pageSize': pageSize,'seeType':2 }]
  getToken.getData(function(res){
    console.log(res.data);
    let lists = res.data.list;
    if (lists.length > 0) {
      for (let i = 0; i < lists.length; i++) {
        lists[i].image = JSON.parse(lists[i].image);
        lists[i].img = 'https://www.globalhexi.com/' + lists[i].image[0].path + '/big/' + lists[i].image[0].id + '.jpg';
        lists[i].intPinPrice = (lists[i].intPinPrice / 100).toFixed(2);
        lists[i].intPrice = (lists[i].intPrice / 100).toFixed(2);
        that.data.list.push(lists[i]);
      }
      pageSize++;
    }
    if (that.data.list.length> 0) {
      that.data.type = 1;
    } else {
      that.data.type = 2;
    }
    that.setData({
      list: that.data.list,
      type: that.data.type,
      hidden: true
    })
  },...params)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    boolT: true,
    disabled: false,
    type:0,
    list:[],
    s: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    pageSize=1;
    openId = wx.getStorageSync('openId') || '';
    memberId = wx.getStorageSync('memberId')||'';
    cookie = wx.getStorageSync('cookie')||'';
    token = wx.getStorageSync('token') || '';
    ckToken(that);
  },
  enter: function (e) {
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../paired_detail/paired_detail?scene=' + e.currentTarget.dataset.id,
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    detail(that);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})